package com.wlts.wzy.KSOrderModule.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryOrderListRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryOrderListResponse;
import com.kuaishou.merchant.open.api.KsMerchantApiException;
import com.kuaishou.merchant.open.api.client.AccessTokenKsMerchantClient;
import com.kuaishou.merchant.open.api.request.KsMerchantOrderDeliverRequest;
import com.kuaishou.merchant.open.api.request.KsMerchantOrderListRequest;
import com.kuaishou.merchant.open.api.response.KsMerchantOrderDeliverResponse;
import com.wlts.wzy.KSOrderModule.entity.KsOrders;
import com.wlts.wzy.KSOrderModule.service.IKsOrdersService;
import com.wlts.wzy.KSUserModule.entity.KsOauth;
import com.wlts.wzy.KSUserModule.entity.KsStoreUser;
import com.wlts.wzy.KSUserModule.service.IKsOauthService;
import com.wlts.wzy.KSUserModule.service.IKsStoreUserService;
import com.wlts.wzy.configBean.KsConfig.KsConfigInfo;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;


@Component
public class LmExpressToKsUtil {
    @Autowired
    private IKsOrdersService ksOrdersService;
    @Autowired
    private IKsStoreUserService ksStoreUserService;
    @Autowired
    private IKsOauthService ksOauthService;

    /**
     * 在lm获得发货订单--更新订单
     */
    @Scheduled(fixedDelay = 60 * 1000)
//    @PostConstruct
    public void LmUpdateOrdersToLocal(){

        MyLambdaQueryWrapper<KsStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(KsStoreUser::getDelFlag, "0");

        userWrapper.isNotNull(KsStoreUser::getKsSellerId);

        List<KsStoreUser> ksOauthList = ksStoreUserService.list(userWrapper);

        for (KsStoreUser user : ksOauthList
        ) {
            MyLambdaQueryWrapper<KsOrders> queryWrapper = new MyLambdaQueryWrapper<>();

            queryWrapper.in(KsOrders::getSelfOrder, 1);//平台商品的订单 0未确定 1是 2不是

            queryWrapper.eq(KsOrders::getPutLmStatus, 1);//推送lm状态 0未推送 1已推送 2推送失败

            queryWrapper.eq(KsOrders::getSendStatus, 1);//lm发货状态 0发货 1已发货

            queryWrapper.eq(KsOrders::getPutExpressStatus, 0);//推送物流状态 0未推送，1已经推送 2推送失败

            queryWrapper.eq(KsOrders::getStatus, 30);

            queryWrapper.eq(KsOrders::getSellerId, user.getKsSellerId());

            queryWrapper.eq(KsOrders::getSendStatus, 1);

            queryWrapper.isNotNull(KsOrders::getLmOrderId);

            queryWrapper.isNotNull(KsOrders::getExpressNo);

            MyLambdaQueryWrapper<KsOauth> oauthWrapper = new MyLambdaQueryWrapper<>();

            oauthWrapper.eq(KsOauth::getDelFlag, "0");

            oauthWrapper.eq(KsOauth::getUserUuid, user.getUuid());

            KsOauth ksOauth = ksOauthService.getOne(oauthWrapper);


            AccessTokenKsMerchantClient tokenKsMerchantClient
                    = new AccessTokenKsMerchantClient("", KsConfigInfo.appKey, KsConfigInfo.signSecret);

            int page = 1;
            int pageSize = 15;

            while (true) {

                List<KsOrders> ksOrdersList = ksOrdersService.page(queryWrapper, page, pageSize, true).getRecords();

                ksOrdersList.forEach(ksOrders -> {

                    KsMerchantOrderDeliverRequest orderDeliverRequest = new KsMerchantOrderDeliverRequest();
                    orderDeliverRequest.setAccessToken(ksOauth.getAccessToken());
                    orderDeliverRequest.setOrderId(ksOrders.getOid());
                    orderDeliverRequest.setExpressCode(ksOrders.getExpressCode());
                    orderDeliverRequest.setExpressNo(ksOrders.getExpressNo());

                    KsMerchantOrderDeliverResponse response = null;
                    try {
                        response = tokenKsMerchantClient.execute(orderDeliverRequest);

                        System.out.println(response.toString());

                        if (response.getResult() == 1) {
                            ksOrders.setPutExpressStatus(1);
                        } else {
                            ksOrders.setPutExpressStatus(2);
                        }

                    } catch (KsMerchantApiException e) {
                        e.printStackTrace();
                    }
                    ksOrders.setKsErrMsg(JSON.toJSONString(response));

                });
                if (ksOrdersList.size() > 0) {
                    ksOrdersService.updateBatchById(ksOrdersList);
                }

                if (ksOrdersList.size() < pageSize) {
                    break;
                }
                page += 1;
            }
        }

    }

}
